package com.itcast.tcp.server.decoder_tcp;

import com.itcast.tcp.protocol.MyProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

/**
 * @program: bio_tcp
 * @description:
 * @author: zhanghz001
 * @create: 2021-02-23 11:31
 **/
public class TcpPackageDecoder extends ReplayingDecoder<ByteBuf> {
    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext,
                          ByteBuf byteBuf, List<Object> list) throws Exception {
        int length = byteBuf.readInt();
        byte[] bytes = new byte[length];
        byteBuf.readBytes(bytes);
        MyProtocol protocol = new MyProtocol();
        protocol.setLength(length);
        protocol.setBytes(bytes);
        list.add(protocol);
    }
    
}
